package com.ruoyi.web.controller.examine.example;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.BuffetExamineService;
import com.ruoyi.system.service.ExampleService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.RestaurantGoodsService;
import com.ruoyi.system.service.resp.NormalExamineDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/examine/goods/example")
public class ExampleController extends BaseController {
    @Autowired
    private ExampleService exampleService;

    @Autowired
    private RestaurantGoodsService restaurantGoodsService;

    @Resource
    private ISysUserService sysUserService;
    @Log(title = "自助餐质检", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(Example examinePlan)
    {
        startPage();
        List<Example> list = exampleService.selectList(examinePlan);
        return getDataTable(list);
    }

    @Log(title = "10条留样", businessType = BusinessType.OTHER)
    @GetMapping("/tenList")
    public AjaxResult tenList()
    {
        List<Example> list = exampleService.selectTenList();
        return AjaxResult.success(list);
    }




    @Log(title = "自助餐质检All", businessType = BusinessType.OTHER)
    @GetMapping("/listAll")
    public TableDataInfo listAll(Example examinePlan)
    {
        List<Example> list = exampleService.selectList(examinePlan);
        return getDataTable(fillToDTO(list));
    }
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success((exampleService.selectById(id)));
    }

    @Log(title = "自助餐质检", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Example examinePlan)
    {
        examinePlan.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = exampleService.create(examinePlan);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "自助餐质检", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Example examinePlan) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (!examinePlan.getArea().equals(sysUser.getLocation())) {
            return error("您不能修改当前单据！");
        }
        examinePlan.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = exampleService.update(examinePlan);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "自助餐质检", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        Example example = exampleService.selectById(id);
        if (!example.getArea().equals(sysUser.getLocation())) {
            return error("您不能修改当前单据！");
        }
        return toAjax(exampleService.deleteById(id));
    }


    private List<Example> fillToDTO(List<Example> normalExamines) {
        if (CollectionUtils.isEmpty(normalExamines)) {
            return new ArrayList<>();
        }
        List<Long> goodsId = normalExamines.stream().map(Example::getGoodsId).collect(Collectors.toList());

        Map<Long, RestaurantGoods> restaurantGoodsMap = restaurantGoodsService.selectByIds(goodsId).stream().collect(Collectors.toMap(RestaurantGoods::getId, g -> g));
        return normalExamines.stream().map(g -> {

            RestaurantGoods keeper = restaurantGoodsMap.get(g.getGoodsId());
            if (keeper != null) {
                g.setGoodsName(keeper.getName());
            }
            return g;
        }).collect(Collectors.toList());
    }

}
